package com.thinkgem.jeesite.modules.book.web;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.book.entity.BkBookinfo;
import com.thinkgem.jeesite.modules.book.entity.SelfBookSearch;
import com.thinkgem.jeesite.modules.book.service.BookMgrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by liu_pc on 2015-12-07.
 */

@Controller
@RequestMapping(value = "bookinfo")
public class BookController  extends BaseController {

    @Autowired
    private BookMgrService bookMgrService;

    @ModelAttribute
    public BkBookinfo get(@RequestParam(required = false) String id){
        if(StringUtils.isNotBlank(id)){
            return bookMgrService.get(id);
        }
        else{
            return new BkBookinfo();
        }
    }

    @RequestMapping("")
    public String search(){
        return "modules/book/front/search";
    }

    @RequestMapping(value = {"getBookinfo"})
    public String list(BkBookinfo bkBookinfo, Model model){
        model.addAttribute("bkBookinfo", bkBookinfo);
        return "modules/book/front/bookInfoList";
    }

    @RequestMapping(value = {"getBookinfoByPage"})
    @ResponseBody
    public Page<BkBookinfo> getBookinfoByPage(String bookname,
                                              String author,
                                              String classcode,
                                              String publisher,
                                              HttpServletRequest request,
                                              HttpServletResponse response){
        SelfBookSearch search = new SelfBookSearch();
        search.setBookname(bookname);
        search.setAuthor(author);
        search.setClasscode(classcode);
        search.setPublisher(publisher);
        search.setRemarks("0");
        return bookMgrService.findFrontSearchPage(new Page<>(request, response), search);
    }


    @RequestMapping("detail")
    public String detail(HttpServletRequest request, Model model){
        String bookId = request.getParameter("id");
        BkBookinfo bkBookinfo = bookMgrService.findFrontSearchByBookId(bookId);
        model.addAttribute("bkBookinfo", bkBookinfo);
        return "modules/book/front/bookform";
    }
}
